home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * ArrayList.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
- if (!IS.isModuleInitialized("IS.NOF.UTIL.ArrayList"))
- {
- /****h* NOF_JavaScript_Library/NOF.UTIL.ListElement
- *
- * NAME
- * NOF.UTIL.ListElement
- *
- * DESCRIPTION
- * Object with a value and a display value for select or list control elements.
- *
- ****/
- function UTIL_ListElement( value, displayValue ) {
- this.__proto__ = UTIL_ListElement.prototype;
- this.value = value;
- this.displayValue = displayValue;
- }
- {
- var method = UTIL_ListElement.prototype;
- /**
- * Equals method
- * @param elem - the element to compare with
- **/
- method.equals = function ( /*NOF.UTIL.ListElement*/ elem ) {
- if (this.value == elem.value)
- return true;
- else
- return false;
- }
- }
-
- UTIL.__proto__.ListElement = UTIL_ListElement;
-
- /****h* NOF_JavaScript_Library/NOF.UTIL.ArrayList
- *
- * NAME
- * NOF.UTIL.ArrayList
- *
- * DESCRIPTION
- * The javascript array implementation extended with methods of the List interface
- *
- ****/
-
- /**
- * Constructor
- * @param arr - initial array to construct the list from. Can be null.
- **/
- function UTIL_ArrayList( /*Array*/ arr ) {
- if ( arr != null )
- this._list = arr;
- else
- this._list = new Array();
- }
- {
- var member = UTIL_ArrayList.prototype;
- member._list = null;
-
- var method = UTIL_ArrayList.prototype;
-
- /**
- * Replaces the element at the specified position in this list with the specified element.
- **/
- method.set = function ( /*int*/ index, /*Object*/ elem ){
- this._list[index] = elem;
- }
-
- /**
- * Returns the element at the specified position in this list.
- **/
- method.get = function ( /*int*/ index ){
- return this._list[index];
- }
-
- /**
- * Inserts the specified element at the specified position in this list.
- * If no index specified, element is added to the end of the list.
- **/
- method.add = function ( /*[int*/ arg1 /*]*/, /*Object*/ arg2 ){
- if (arguments.length == 2)
- this._list[arg1] = arg2;
- else
- this._list[this._list.length] = arg1;
- }
-
- /**
- * Appends all of the elements in the specified array to the end of
- * this list, in the order that they are in the array <br>
- * @param col - an array to be inserted into the list
- **/
- method.addAll = function ( /*Array*/ col ){
- var array = col.toArray();
- for (var i=0; i<array.length;i++ )
- this.add( array[i] );
- }
-
- /**
- * Searches for the first occurence of the given argument <br>
- * Test for equality uses the equals method or '==' if isSimpleObject is true <br>
- * @param elem - an object.
- * @param isSimpleObject - true if object is using '==' and not 'equals' to test equality
- *
- **/
- method.indexOf = function ( /*Object*/ elem, /*boolean*/ isSimpleObject ) {
- if (elem != null)
- for( var i=0;i<this._list.length;i++) {
- var isEqual;
- if (arguments.length == 2 && isSimpleObject)
- isEqual = (this._list[i] == elem) ? true : false;
- else
- isEqual = this._list[i].equals (elem);
- if (isEqual)
- return i;
- }
- return -1;
- }
-
- /**
- * Searches for the first occurence of the given argument, testing for equality using the equals method
- * or '==' if isSimpleObject is true. Kept for compatibility with older versions.
- **/
- method.getIndex = method.indexOf;
-
- /**
- * Returns true if this list contains the specified element.
- * <br>
- * Test for equality uses the equals method or '==' if isSimpleObject is true <br>
- * @param elem - an object.
- * @param isSimpleObject - true if object is using '==' and not 'equals' to test equality
- *
- **/
- method.contains = function ( /*Object*/ elem, /*boolean*/ isSimpleObject ){
- if (this.getIndex(elem, isSimpleObject) != -1)
- return true;
- return false;
- }
-
- /**
- * Removes the element at the specified position in this list.
- **/
- method.remove = function ( /*int*/ index ){
- for (var j=index; j<this._list.length-1; j++)
- this._list[j] = this._list[j+1];
-
- this._list.length--;
- }
-
- /**
- * Swaps two elements in the list
- **/
- method.swap = function ( /*int*/ fromIndex, /*int*/ toIndex){
- var tmp = this._list[fromIndex];
- this._list[fromIndex] = this._list[toIndex];
- this._list[toIndex] = tmp;
- }
-
- /**
- * Move the items from left to right (that is from 0 -> length)
- * with last value getting first (arr[length]->arr[0])
- **/
- method.shiftToRight = function () {
- for (var i=this._list.length-1;i>0; i--) {
- this.swap (i,i-1);
- }
- }
-
- /**
- * Move the items from right to left (that is from length -> 0)
- * with first value getting last (arr[0]->arr[length])
- **/
- method.shiftToLeft = function () {
- for (var i=1; i<this._list.length; i++) {
- this.swap (i-1,i);
- }
- }
-
- /**
- * Move the item one element to the right or the left (depending on direction +/-)
- **/
- method.shiftItem = function (/*int*/ index, /*int*/ direction) {
- var newIndex = index + direction;
- if (newIndex < 0) {
- this.shiftToLeft();
- }
- else if (newIndex > this._list.length-1) {
- this.shiftToRight();
- }
- else {
- this.swap (newIndex, index);
- }
- }
-
- /**
- * Move the item 'n' elements to the right or the left (depending if n is +/-)
- **/
- method.shiftItemWithN = function (/*int*/ index, /*int*/ n) {
- var direction = (n > 0) ? +1 : -1;
- for (var i=0; i<Math.abs(n);i++) {
- var newIndex = index + i*direction;
- this.shiftItem (newIndex,direction);
- }
- }
-
- /**
- * Tests if this list has no elements.
- **/
- method.isEmpty = function (){
- if (this._list.length == 0 )
- return true;
- else
- return false;
- }
-
- /**
- * Removes all of the elements from this list.
- **/
- method.clear = function (){
- this._list = new Array();
- }
-
- /**
- * Returns the number of elements in this list.
- **/
- method.size = function (){
- return this._list.length;
- }
-
- /**
- * Sorts the list using the JavaScript Array.sort function
- **/
- method.sort = function () {
- this._list.sort();
- }
-
- /**
- * Returns an array containing all of the elements in this list in the correct order.
- **/
- method.toArray = function (){
- return this._list;
- }
- }
-
- UTIL.__proto__.ArrayList = UTIL_ArrayList;
- }